package com.example.amoxicilin.null_test.main;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.amoxicilin.null_test.R;
import com.example.amoxicilin.null_test.tools.FileDownload;
import com.example.amoxicilin.null_test.tools.HttpUtils;

public class StudyActivity extends AppCompatActivity {

    private TextView textView;
    private int id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        textView=(TextView)findViewById(R.id.study_content);
        Bundle bundle=getIntent().getExtras();
        String text=bundle.getString("Position");
        id=bundle.getInt("ResourceId");
        textView.setText(" "+text);

        FloatingActionButton study_fab = (FloatingActionButton) findViewById(R.id.study_fab);
        study_fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "确认下载?", Snackbar.LENGTH_LONG)
                        .setAction("开始", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                String getResourcepath="http://192.168.12.221:8080/ContentServlet?classid="+id;
                                String submitString = HttpUtils.getJsonContent(getResourcepath);
                                Log.i("xiazai", "onClick: "+submitString);
                                FileDownload.downLoad(submitString,getFileName(submitString));
                            }
                        }).show();
            }
        });
    }
    public String getFileName(String pathandname) {
        int start = pathandname.lastIndexOf("/");
        int end = pathandname.length();
        if (start != -1 && end != -1) {
            return pathandname.substring(start + 1, end);
        } else {
            return null;
        }

    }
}
